<?php
class Admin_Model extends CI_Model {

	var $id;
	var $gmt_create;
	var $creator = '11';
	var $gmt_modified;
	var $modifier = '11';
	var $user_name;
	var $password;
	var $email;
	var $role_id;
	var $gmt_last_login;
	var $last_ip;

	public function __construct() {
		$this->load->database();
	}

	public function authenticate() {

		//echo "admin:" . $this->user_name . " password:" . $this->password ;

		$admin_model = $this->getByUserName($this->user_name);

		//echo "adfasfad";

		if($admin_model == null) {
			return "";
		}

		//echo "1111" . $admin_model->password . " 2222" . $this->password ;

		if($admin_model->password == $this->password) {
			return $admin_model->id . "" ;
		} else {
			return "";
		}
	}

	public function getAdmins() {
		$query = $this->db->get('admin');
		return $query->row_array();
	}

	public function getByUserName($userName) {
		$query = $this->db->get_where('admin', array('user_name'=>$userName));
		return $query->row();
	}

	public function createAdmin() {

		if($this->gmt_create == null) {
			$this->gmt_create = date("Y-m-d H:i:s", time());
		}

		if($this->gmt_modified == null) {
			$this->gmt_modified = date("Y-m-d H:i:s", time());
		}

		$password = md5($password);

		$this->db->insert('admin', $this);
	}

	public function get($id){
		$query = $this->db->get_where('admin', array('id'=>$id));
		return $query->row();
	}

	public function updateLastLoginInfo($userName, $datetime, $ip) {
		$this->db->update('admin', array('gmt_last_login'=>$datetime, 'last_ip'=>$ip), array('user_name'=>$userName));
	}
}